//
//  XHFindPwdViewController.m
//  xinhua_sx
//
//  Created by 党阳阳 on 14-7-17.
//  Copyright (c) 2014年 dangyangyang. All rights reserved.
//

#import "XHFindPwdViewController.h"
#import "MBProgressHUD.h"
#import "AFNetworking.h"
@interface XHFindPwdViewController ()
{
    MBProgressHUD* HUD;
}
@end

@implementation XHFindPwdViewController
@synthesize username = _username;
@synthesize email = _email;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [self.leftButton setImage:[UIImage imageNamed:@"close"] forState:UIControlStateNormal];
        self.leftButton.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
        @weakify(self);
        NSArray *array = [[NSBundle mainBundle]loadNibNamed:@"FindPwdView" owner:self options:nil];
        UIView *view = [array objectAtIndex:0];
        CGRect rect = self.view.frame;
        CGFloat oY = kIOS_VERSION<7.0?44:64;
        [view setFrame:CGRectMake(0, oY, rect.size.width, rect.size.height-oY)];
        //[self.view addSubview:view];
       self.titleLable.text  =@"找回密码";
        self.rightButton.hidden = YES;
        [[self.leftButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self);
            [self dismissViewControllerAnimated:YES completion:nil];
        }];
        

    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    UILabel* l1 = [[UILabel alloc] initWithFrame:CGRectMake(20, 80, 100, 15)];
    l1.font = [UIFont systemFontOfSize:15];
    l1.backgroundColor = [UIColor clearColor];
    l1.text = @"用户名";
    [self.view addSubview:l1];
    UITextField* t1 = [[UITextField alloc] initWithFrame:CGRectMake(20, 100, 280 ,30)];
    t1.font = [UIFont systemFontOfSize:15];
    t1.borderStyle = UITextBorderStyleLine;
    //t1.secureTextEntry = YES;
    [self.view addSubview:t1];
    
    UILabel* l2 = [[UILabel alloc] initWithFrame:CGRectMake(20, 150, 120, 15)];
    l2.font = [UIFont systemFontOfSize:15];
    l2.backgroundColor = [UIColor clearColor];
    l2.text = @"您注册时的邮箱";
    [self.view addSubview:l2];
    
    UITextField* t2 = [[UITextField alloc] initWithFrame:CGRectMake(20, 170, 280 ,30)];
    t2.font = [UIFont systemFontOfSize:15];
    t2.borderStyle = UITextBorderStyleLine;
   // t2.secureTextEntry = YES;
    [self.view addSubview:t2];
    self.username = t1;
    self.email = t2;
    UIButton* button = [[UIButton alloc] initWithFrame:CGRectMake(20, 220, 280,30)];
    [button setTitle:@"提交" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(tijiao) forControlEvents:UIControlEventTouchUpInside];
    [button setBackgroundColor:[XHFindPwdViewController colorWithHexString:@"#A6C4E8"]];
    [self.view addSubview:button];

    // Do any additional setup after loading the view.
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [_email resignFirstResponder];
    [_username resignFirstResponder];
}
- (BOOL) validateEmail: (NSString *) candidate {
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:candidate];
}
-(void)tijiao
{
    [_email resignFirstResponder];
    [_username resignFirstResponder];
    if ([self.username.text length] == 0 || [self.email.text length] == 0)
    {
        UIAlertView* v1 = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请填写正确信息" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [v1 show];
        return;
    }
    if (![self validateEmail:_email.text])
    {
        UIAlertView* v1 = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请填写正确邮箱" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [v1 show];
        return;
    }
    HUD = [[MBProgressHUD alloc] initWithView:self.view];
    HUD.labelText = @"正在提交";
    HUD.dimBackground = YES;
    [self.view addSubview:HUD];
    [HUD show:YES];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
   
    NSDictionary* dic = @{@"frontUserName":_username.text,@"email":_email.text};
    NSString* sting = [NSString stringWithFormat:@"http://183.203.18.45:7001/xinhua/m/findPassword.html"];
    [manager POST:sting parameters:dic success:^(AFHTTPRequestOperation*operation, id responseObject) {
        id jsonObject = [NSJSONSerialization JSONObjectWithData:operation.responseData
                                                        options:NSJSONReadingAllowFragments
                                                          error:nil];
        NSLog(@"json = %@",jsonObject);
        [HUD hide:YES];
        NSString* string = [NSString stringWithFormat:@"%@",jsonObject[@"result"]];
        if ([string isEqualToString:@"success"])
        {
            UIAlertView* v1 = [[UIAlertView alloc] initWithTitle:@"提示" message:jsonObject[@"message"] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
            [v1 show];
            
            NSLog(@"1222");
           
            NSLog(@"2233");
        }
        else
        {
            UIAlertView* v1 = [[UIAlertView alloc] initWithTitle:@"提示" message:jsonObject[@"message"] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
            [v1 show];
        }
        //self.usreL.text = [jsonObject objectForKey:@"name"];
        //self.nameL.text = [jsonObject objectForKey:@"nickName"];
        //[self.ve setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://183.203.18.45:7001%@",jsonObject[@"qRcodePath"]]] placeholderImage:[UIImage imageNamed:@"base_detail_default_pic.png"]];
    } failure:^(AFHTTPRequestOperation *operation, NSError*error) {
        [HUD hide:YES];
        UIAlertView* v1 = [[UIAlertView alloc] initWithTitle:@"提示" message:@"修改失败!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [v1 show];
        NSLog(@"error = %@",error);
    }];
    
    
    
}
+ (UIColor *) colorWithHexString: (NSString *)color
{
    NSString *cString = [[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    
    // String should be 6 or 8 characters
    if ([cString length] < 6) {
        return [UIColor clearColor];
    }
    
    // strip 0X if it appears
    if ([cString hasPrefix:@"0X"])
        cString = [cString substringFromIndex:2];
    if ([cString hasPrefix:@"#"])
        cString = [cString substringFromIndex:1];
    if ([cString length] != 6)
        return [UIColor clearColor];
    
    // Separate into r, g, b substrings
    NSRange range;
    range.location = 0;
    range.length = 2;
    
    //r
    NSString *rString = [cString substringWithRange:range];
    
    //g
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];
    
    //b
    range.location = 4;
    NSString *bString = [cString substringWithRange:range];
    
    // Scan values
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    
    return [UIColor colorWithRed:((float) r / 255.0f) green:((float) g / 255.0f) blue:((float) b / 255.0f) alpha:1.0f];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
